var setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "id",
            pIdKey: "parentId",
            rootPId: 0
        },
        key: {
            url:"nourl"
        }
    }
};
var ztree;
debugger
console.log("baseURL",(window.localStorage.getItem("baseURL")?window.localStorage.getItem("baseURL"):""))

var vm = new Vue({
    el:'#myapp',
    data:{
        showList: true,
        title: null,
        sysArea: {
            parentId:null,
            parentName:null,
            name:null,
            shortName:null,
            code:null,
            sort:0,
            remarks:null
        },
        parentName:null,
        queryId:null
    },
    methods: {
        fillParentIds: function(){
            //加载菜单树
            $.get((window.localStorage.getItem("baseURL")?window.localStorage.getItem("baseURL"):"") + "sys/sysarea/fillParentIds", function(r){
                if(r.code === 0){
                    toast('操作成功');
                    vm.reload();
                }else{
                    warning(r.msg);
                }
            })
        },getArea: function(){
            //加载菜单树
            $.get((window.localStorage.getItem("baseURL")?window.localStorage.getItem("baseURL"):"") + "sys/sysarea/areaTree", function(jsonArray){
                ztree = $.fn.zTree.init($("#areaTree"), setting, jsonArray);
                var node = ztree.getNodeByParam("id", vm.sysArea.parentId);

                if(node){
                    ztree.selectNode(node);
                    vm.parentName = node.name;
                }
            })
        },
        areaTree: function(){
            layer.open({
                type: 1,
                offset: '50px',
                skin: 'layui-layer-molv',
                title: "选择区域",
                area: ['30%', '90%'],
                shade: 0,
                shadeClose: false,
                content: jQuery("#areaLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var node = ztree.getSelectedNodes();
                    //选择上级菜单
                    vm.sysArea.parentId = node[0].id;
                    vm.parentName = node[0].name;
                    layer.close(index);
                }
            });
        },
       query: function () {
            vm.reload();
        },
        add: function(){
            vm.showList = false;
            vm.title = "新增";
            vm.sysArea = {};
            vm.getArea();
        },
        update: function (event) {
           var id = getAreaId();
            if(id == null){
                return ;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
        },
        saveOrUpdate: function (event) {
            var url = vm.sysArea.id == null ? "sys/sysarea/save" : "sys/sysarea/update";
            $.ajax({
                type: "POST",
                url: (window.localStorage.getItem("baseURL")?window.localStorage.getItem("baseURL"):"") + url,
                contentType: "application/json",
                data: JSON.stringify(vm.sysArea),
                success: function(r){
                    if(r.code === 0){
                        toast('操作成功');
                        vm.reload();
                    }else{
                        warning(r.msg);
                    }
                }
            });
        },
        del: function (event) {
           var ids = [];
           // 需要用数组包住，后台数组接收，否则数组解析异常
           ids.push(getAreaIds());
            if(ids == null){
                return ;
            }

            confirm('确定要删除选中的记录？', function(){
                $.ajax({
                    type: "POST",
                    url: (window.localStorage.getItem("baseURL")?window.localStorage.getItem("baseURL"):"") + "sys/sysarea/del",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function(r){
                        if(r.code == 0){
                            toast('操作成功');
                            vm.reload();
                        }else{
                            warning(r.msg);
                        }
                    }
                });
            });
        },
        // getInfo: function(id){
        //     $.get((window.localStorage.getItem("baseURL")?window.localStorage.getItem("baseURL"):"") + "sys/sysarea/info/"+id, function(r){
        //         vm.sysArea = r.entity;
        //         vm.getArea();
        //     });
        //
        // },
        getInfo: function (id) {
            var url = (window.localStorage.getItem("baseURL")?window.localStorage.getItem("baseURL"):"") + "sys/sysarea/info/" + id;
            com.AjaxGet(url, null, "json", function (r) {
                vm.sysArea = r.entity;
                vm.getArea();
            }, null, false)
        },
        reload: function (event) {
            vm.showList = true;
            Dept.parentId = this.queryId;


            Dept.table.url = getUrl();


            console.log(getUrl());
            console.log(Dept.table);
            Dept.table.refresh();
        }
    }
});


//tree表格

var Dept = {
    id: "areaTable",
    table: null,
    layerIndex: -1,
    parentId:0,
};

function getAreaId () {
    var selected = $('#areaTable').bootstrapTreeTable('getSelections');
    if (selected.length == 0) {
        alert("请选择一条记录");
        return null;
    } else {
        return selected[0].id;
    }
}

function getAreaIds () {
    var selected = $('#areaTable').bootstrapTreeTable('getSelections');
    if (selected.length == 0) {
        alert("请选择一条记录");
        return null;
    } else {
        return selected[0].id;
    }
}

/**
 * 初始化表格的列
 */
Dept.initColumn = function () {
    var columns = [
        {field: 'selectItem', radio: true},
        {title: '编码', field: 'id', visible: false, align: 'center', valign: 'middle', width: '80px'},
        {title: '名称', field: 'name', align: 'center', valign: 'middle', sortable: true, width: '180px'},
        {title: '短名称', field: 'shortName', align: 'center', valign: 'middle', sortable: true, width: '180px'},
        {title: '上级地区', field: 'parentName', align: 'center', valign: 'middle', sortable: true, width: '100px'},
        {title: '排序号', field: 'orderNum', align: 'center', valign: 'middle', sortable: true, width: '100px'}]
    return columns;
};

function getUrl(){
    return (window.localStorage.getItem("baseURL")?window.localStorage.getItem("baseURL"):"") + "sys/sysarea/areaLevels?parentId="+Dept.parentId;
}
var table;
$(function () {
    var colunms = Dept.initColumn();
    table = new TreeTable(Dept.id, getUrl(), colunms);
    table.setRootCodeValue(Dept.parentId);
    table.setExpandColumn(2);
    table.setIdField("id");
    table.setCodeField("id");
    table.setParentCodeField("parentId");
    table.setExpandAll(false);
    table.setHeight("100%");
    table.init();
    Dept.table = table;
});